<?php

use swap\lazy_checker;
use swap\check_failed;
use swap\visitor;

class letter_controller extends swap\controller {
    
    public static $layout = 'my';
    
    public static function before_run() {
        self::role('user', 'redirect_to', 'my-site/login');
    }
    
    public static function index_action() {
        // 获取已有的信
        $letters = letter_model::get_letters_by_user_id(visitor::get_role_id('user'));
        self::set('letters', $letters);
        
        // 呈现
        self::show_page();
    }
    
    public static function new_action() {
        self::send_block('my/letter_form');
    }
    
    public static function do_new_action() {
        // 拦截
        self::method('post');
        self::csrf('user');

        try {
            // 校验
            $props = self::check_form(p(), 'new');
            
            // 检查是否满
            letter_service::can_new_letter();
            
            // 执行
            letter_model::add($props);
            
            // 正常
            self::json_result(true, '新建成功');
        } catch (letter_except $se) {
            // 异常1
            self::json_result(false, $se->get_msg(), 0, $se->get_prop_name());
        } catch (check_failed $e) {
            // 异常2
            $msg = $e->get_reasons();
            self::json_result(false, current($msg), 0, key($msg));
        }
    }
    
    public static function edit_action() {
        // 校验letter_id
        $letter = self::check_id();
        self::set('letter', $letter);
        
        // 呈现
        self::send_block('my/letter_form');
        
    }
    
    public static function do_edit_action() {
        // 校验letter_id
        $letter = self::check_id();
        
        // 拦截
        self::method('post');
        self::csrf('user');

        try {
            // 校验
            $props = self::check_form(p(), 'edit');
            
            // 执行
            letter_model::set_by_id($props, $letter->id);
            
            // 正常
            self::json_result(true, '编辑成功');
        } catch (check_failed $e) {
            // 异常
            $msg = $e->get_reasons();
            self::json_result(false, current($msg), 0, key($msg));
        }
    }
    
    public static function del_action() {
        // 校验letter_id
        $letter = self::check_id();
        self::set('letter', $letter);
        
        // 呈现
        self::send_page();
    }
    
    public static function do_del_action() {
        // 校验letter_id
        $letter = self::check_id();
        
        // 拦截
        self::method('post');
        self::csrf('user');
        
        // 执行
        letter_model::del_by_id($letter->id);
        
        // 正常
        self::send_json(true, '删除成功');
    }
    
    public static function preview_action() {
        // 校验letter_id
        $letter = self::check_id();
        self::set('letter', $letter);
        
        self::send_page();
    }
    
    public static function do_preview_action() {
        self::forward_to('my-letter/edit');
    }
    
    public static function copy_action() {
        // none
    }
    
    public static function do_copy_action() {
        // 校验letter_id
        $letter = self::check_id();
        
        // 拦截
        self::method('post');
        self::csrf('user');

        try {
            // 检查是否满
            letter_service::can_new_letter();
            
            // 执行
            $props = array(
                'user_id' => $letter->user_id,
                'title' => $letter->title,
                'content' => $letter->content,
                'update_time' => time()
            );
            dump($props);
            letter_model::add($props);
            
            // 正常
            self::json_result(true, '复制成功');
        } catch (letter_except $se) {
            // 异常1
            self::json_result(false, $se->get_msg(), 0, $se->get_prop_name());
        } catch (check_failed $e) {
            // 异常2
            $msg = $e->get_reasons();
            self::json_result(false, current($msg), 0, key($msg));
        } 
    }
    
    protected static function check_form($post, $operation) {
        $checker = new lazy_checker($post);
        $checker->check_model_rules('letter', $operation);
        $props = array(
            'user_id' => visitor::get_role_id('user'),
            'update_time' => time(),
            'title' => $checker->title,
            'content' => $checker->content,
        );
        return $props;
    }
    protected static function check_id() {
        $id = g_int('id');
        $letter = letter_model::get_by_id($id);
        if ($letter === null) {
            self::forward_404('非法请求');
        }
        return $letter;
    }
}